package com.saunter.service;

import com.saunter.extendMapper.DepartmentExtendMapper;
import com.saunter.extendModel.DepartmentExtend;
import com.saunter.mapper.DepartmentMapper;
import com.saunter.model.Department;
import com.saunter.utils.exception.ExceptionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("departmentService")
public class DepartmentService {
    @Autowired
    private DepartmentExtendMapper departmentExtendMapper;
    @Resource
    private ExceptionManager exceptionManager;

    public int departmentAdd(Department department) {
        Integer department_status = department.getStatus();
        if(null != department_status && 0 != department_status && 1 != department_status) {
            throw exceptionManager.create("Prm003");
        }else {
            if(null != departmentExtendMapper.getByName(department.getName())) {
                throw exceptionManager.create("Dep002");
            }else {
                return departmentExtendMapper.insertSelective(department);
            }
        }
    }

    public Department getById(int department_id) {
        return departmentExtendMapper.selectByPrimaryKey(department_id);
    }

    public List<DepartmentExtend> getAllDepartmentJobs() {
        return departmentExtendMapper.getAllDepartmentJobs();
    }

    public int editName(Department new_department) {
        if(null == this.getById(new_department.getId())) {
            throw exceptionManager.create("Dep001");
        }else{
            return departmentExtendMapper.updateByPrimaryKeySelective(new_department);
        }
    }

    public int delete(int department_id) {
        DepartmentExtend departmentExtend = departmentExtendMapper.getDepartmentJobsById(department_id);
        if(null == departmentExtend) {
            throw exceptionManager.create("Dep001");
        }else {
            if(0 == departmentExtend.getJobs().size()) {
                return departmentExtendMapper.deleteByPrimaryKey(department_id);
            }else {
                throw exceptionManager.create("Dep003");
            }
        }
    }
}
